/*
 ------------------------------------------------------------------------------
 Copyright (C) 2006-2007 Team Blur.

 This file is part of the Quantum Engine source code.

 The Quantum Engine source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The Quantum Engine source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the Quantum Engine source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


#include "client.h"


#define FPS_FRAMES		4


/*
 ==================
 CL_DrawChar
 ==================
*/
static void CL_DrawChar (int x, int y, int w, int h, int c){

	float	col, row;

	if (c == ' ')
		return;

	col = (c & 15) * 0.0625f;
	row = (c >> 4) * 0.0625f;

	R_DrawStretchPic(x, y, w, h, col, row, col + 0.0625f, row + 0.0625f, cls.charsetMaterial);
}


/*
 ==================
 CL_DrawFPS
 ==================
*/
static void CL_DrawFPS (void){

	static int	frameTimes[FPS_FRAMES];
	static int	frameIndex;
	char		string[16];
	int			total, fps;
	int			len, ofs;
	int			i;

	if (!cl_showFPS->integerValue)
		return;

	frameTimes[frameIndex % FPS_FRAMES] = com_frameMsec;
	frameIndex++;

	// Average multiple frames together to smooth changes out a bit
	total = 0;

	for (i = 0; i < FPS_FRAMES; i++)
		total += frameTimes[i];

	if (total < 1)
		total = 1;

	fps = 1000 * FPS_FRAMES / total;
	if (fps > 1000)
		fps = 1000;

	Str_SPrintf(string, sizeof(string), "%i FPS", fps);

	len = Str_LengthWithoutColors(string) * 16;

	// Draw the shadow
	ofs = (SCREEN_WIDTH - len) - 4;

	R_SetColor(colorTable[COLOR_BLACK]);

	for (i = 0; string[i]; i++, ofs += 16){
		if (string[i] == ' ')
			continue;

		CL_DrawChar(ofs + 2, 4 + 2, 16, 16, string[i]);
	}

	// Draw the string
	ofs = (SCREEN_WIDTH - len) - 4;

	R_SetColor(colorTable[COLOR_WHITE]);

	for (i = 0; string[i]; i++, ofs += 16){
		if (string[i] == ' ')
			continue;

		CL_DrawChar(ofs, 4, 16, 16, string[i]);
	}
}

/*
 ==================
 CL_UpdateScreen

 This is called every frame, but can also be called explicitly to flush stuff
 to the screen
 ==================
*/
void CL_UpdateScreen (void){

	if (cls.state == CS_UNINITIALIZED)
		return;		// Nothing running on the client

	// Set the active GUI if not active yet
	
	if (cls.state == CS_DISCONNECTED)
		GUI_Activate(GUI_MAIN);
	else if (cls.state < CS_LOADING)
		GUI_Activate(GUI_CONNECTING);
	

	// Begin frame
	R_BeginFrame(cls.frameTime);

	switch (cls.state){
	case CS_DISCONNECTED:
	case CS_CONNECTED:
	case CS_LOADING:
	case CS_PRIMED:
		// Draw the connecting/loading GUI
		GUI_Refresh(cls.frameTime);

		break;
	case CS_ACTIVE:
		// Draw the game view unless the GUI covers the entire screen
		if (!GUI_IsFullscreen()){
			cg->Refresh();
		}

		// Draw the in-game GUI, if visible
		GUI_Refresh(cls.frameTime);

		break;
	}

	// The console will be drawn on top of anything
	Con_DrawConsole();

	// The FPS display will be drawn on top of anything, even the console
	CL_DrawFPS();

	// End frame
	R_EndFrame();
}
